#include <stdio.h>  
#include <stdlib.h>  
#include <time.h>
#include <unistd.h>
  
void time1() 
{  
    FILE *fp = fopen("111.txt", "w+");
    if (NULL == fp) 
    {  
        perror("打开文件失败\n");
        return;  
    }  
  
    while (1) 
    {  
        time_t now;  
        time(&now);  
        struct tm *t = localtime(&now);  
  
        fprintf(fp, "%d-%02d-%02d %02d:%02d:%02d\n",  
                t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,  
                t->tm_hour, t->tm_min, t->tm_sec);  
  
        fflush(fp); // 确保数据写入文件  
        sleep(1);   // 等待一秒  
    }  
  
    fclose(fp);  
}  
  
int main(int argc, char *argv[]) 
{  
    time1();  
    return 0;  
}